//
//  UIView+HDJiamubiaoView.m
//  app
//
//  Created by apple on 2017/7/18.
//  Copyright © 2017年 hdaren. All rights reserved.
//

#import "HDJiamubiaoView.h"
#import "HDJiamubiaoModel.h"


@interface HDJiamubiaoView ()

@property (nonatomic,strong)NSMutableArray * model_array;

@end

@implementation  HDJiamubiaoView

-(id)initWithParams:(NSMutableArray *)array
{
    if (self = [super init]) {
        
        self.frame = [UIApplication sharedApplication].keyWindow.screen.bounds;
        self.model_array = array;
        
        [self initUI];
        
    }
    
    return self;
    
}


-(void)initUI
{
    self.userInteractionEnabled = YES;
    
    transparentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), CGRectGetHeight([UIScreen mainScreen].bounds))];
    transparentView.backgroundColor = [UIColor blackColor];
    transparentView.userInteractionEnabled = YES;
    [transparentView setMultipleTouchEnabled:YES];
    transparentView.alpha = 0.0f;
    [self addSubview:transparentView];
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideEditDeleteActionSheet)];
    tap.numberOfTapsRequired = 1;
    //    [transparentView addGestureRecognizer:tap];
    
    
    UIButton * btn = [[UIButton alloc]initWithFrame:transparentView.frame];
    [btn addTarget:self action:@selector(hideJiamubiaoViewSheet) forControlEvents:UIControlEventTouchUpInside];
    //    [transparentView addSubview:btn];
    
    
    
    CGFloat sheetWidth = UBO_SCREEN.width - 12*2;
    actionSheetHeight = (self.model_array.count+1) * 40 + 50;
    jiamubiaoView = [[UIView alloc] initWithFrame:CGRectOf([self.ubo fx:UB_1_2 ox:-sheetWidth/2 fy:UB_1_2 oy:-actionSheetHeight/2], [self.ubo ow:sheetWidth oh:actionSheetHeight])];
    jiamubiaoView.backgroundColor = [UIColor whiteColor];
    jiamubiaoView.layer.masksToBounds = YES;
    jiamubiaoView.layer.cornerRadius = UF(10);
    jiamubiaoView.alpha = 0.95;
    [self addSubview:jiamubiaoView];
    
    
    __block CGRect frame = CGRectZero;
    
    frame = jiamubiaoView.frame;
    frame.size.height = actionSheetHeight;
    jiamubiaoView.frame = frame;
    
    
    for (int i = 0; i < self.model_array.count+1; i++) {
        
        UIView * row_view = [[UIView alloc]initWithFrame:CGRectOf([jiamubiaoView.ubo fx:0 fy:0 oy:i*40], [jiamubiaoView.ubo ow:jiamubiaoView.frame.size.width oh:40])];
        [jiamubiaoView addSubview:row_view];
        
        CGFloat width = jiamubiaoView.frame.size.width/3;
        
        UILabel * xiangmu = [[UILabel alloc]initWithFrame:CGRectOf([row_view.ubo fx:0 fy:0], [row_view.ubo ow:width oh:40])];
        [xiangmu setFont:[UIFont systemFontOfSize:13]];
        [xiangmu setTextAlignment:NSTextAlignmentCenter];
        [xiangmu setTextColor:[XCommon hexStringToColor:kColor_l_light_gray_text]];
        [row_view addSubview:xiangmu];
        
        UILabel * danwei = [[UILabel alloc]initWithFrame:CGRectOf([xiangmu.ufo fx:1 fy:0 ], [row_view.ubo ow:width oh:40])];
        [danwei setFont:[UIFont systemFontOfSize:13]];
        [danwei setTextAlignment:NSTextAlignmentCenter];
        [danwei setTextColor:[XCommon hexStringToColor:kColor_l_light_gray_text]];
        [row_view addSubview:danwei];
        
        UILabel * jiage = [[UILabel alloc]initWithFrame:CGRectOf([row_view.ubo fx:1 ox:-width fy:0], [row_view.ubo ow:width oh:40])];
        [jiage setFont:[UIFont systemFontOfSize:13]];
        [jiage setTextAlignment:NSTextAlignmentCenter];
        [jiage setTextColor:[XCommon hexStringToColor:kColor_l_light_gray_text]];
        [row_view addSubview:jiage];
        
        
        if (i == 0) {
        
            [jiage setTextColor:[UIColor redColor]];
            [xiangmu setTextColor:[UIColor redColor]];
            [danwei setTextColor:[UIColor redColor]];
            
            xiangmu.text = @"项目";
            danwei.text = @"单位";
            jiage.text = @"报价(元)";
            
            
        }else
        {
            
            HDJiamubiaoModel * model = [self.model_array objectAtIndex:i-1];
            
            xiangmu.text = model.name;
            danwei.text = model.unit;
            jiage.text = [NSString stringWithFormat:@"%@",model.price];
            
            
        }
        
        UIView * line = [[UIView alloc]initWithFrame:CGRectOf([row_view.ubo fx:0 fy:1 py:-1], [row_view.ubo ow:jiamubiaoView.frame.size.width ph:1])];
        line.backgroundColor = [XCommon hexStringToColor:kColor_gray_line];
        [row_view addSubview:line];
        
        
        
        
        
    }
    
    
    UIView * line_1 = [[UIView alloc]initWithFrame:CGRectOf([jiamubiaoView.ubo fx:UB_1_3 fy:0], [jiamubiaoView.ubo pw:1 oh:280])];
    line_1.backgroundColor = [XCommon hexStringToColor:kColor_gray_line];
    [jiamubiaoView addSubview:line_1];
    
    
    UIView * line_2 = [[UIView alloc]initWithFrame:CGRectOf([jiamubiaoView.ubo fx:UB_2_3 fy:0], [jiamubiaoView.ubo pw:1 oh:280])];
    line_2.backgroundColor = [XCommon hexStringToColor:kColor_gray_line];
    [jiamubiaoView addSubview:line_2];
    
    
    
    UIButton * knew_btn = [[UIButton alloc]initWithFrame:CGRectOf([jiamubiaoView.ubo fx:0 fy:1 oy:-44], [jiamubiaoView.ubo ow:jiamubiaoView.frame.size.width oh:44])];
    [knew_btn setTitle:@"我知道了" forState:UIControlStateNormal];
    [knew_btn setTitleColor:[XCommon hexStringToColor:kColor_l_light_gray_text] forState:UIControlStateNormal];
    [knew_btn addTarget:self action:@selector(know) forControlEvents:UIControlEventTouchUpInside];
    [jiamubiaoView addSubview:knew_btn];
    
    
    
    
}

-(void)know
{
        [self hideJiamubiaoViewSheet];

}


- (void)cancleEvent:(id)sender
{
    
    
    
    [self hideJiamubiaoViewSheet];
}


#pragma mark -
#pragma mark PickerView show/hide

- (void)hideJiamubiaoViewSheet
{
    
    NSLog(@"11111");
    
    
//    [self.timer invalidate];
//    self.timer = nil;
//    [self.send_code_btn setTitle:@"获取验证码" forState:UIControlStateNormal];
//    self.send_code_btn.enabled = YES;
//    i = 1;
    
    [UIView animateWithDuration:0.2
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^() {
                         transparentView.alpha = 0.0;
                         
                         //                         self.center = CGPointMake(CGRectGetWidth(self.frame) / 2.0, CGRectGetHeight([UIScreen mainScreen].bounds) + CGRectGetHeight(self.frame) / 2.0);
                         
                         //self.frame = CGRectMake(0, KDeviceHeight, kDeviceWidth, KDeviceHeight - 64);
                         
                         [transparentView removeFromSuperview];
                         [self removeSelf];
                         
                     } completion:^(BOOL finished) {
                         
                         
                         
                     }];
}

- (void)removeSelf
{
    [self removeFromSuperview];
    
//    self.isShow = NO;
    
}

- (void)showJiamubiaoViewSheet
{
    [self showJiamubiaoViewInView:kWindow];
}

- (void)showJiamubiaoViewInView:(UIView *)view
{
    //    CGFloat vH = CGRectGetHeight(view.frame);
    
    self.frame = CGRectMake(0, 0, UBO_SCREEN.width, UBO_SCREEN.height);
    //    actionSheetY = vH - actionSheetHeight;
    
    [view addSubview:self];
    //    [view insertSubview:transparentView belowSubview:self];
    
//    self.isShow = YES;
    
    [UIView animateWithDuration:0.2
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^() {
                         transparentView.alpha = 0.4;
                         //                         self.frame = CGRectMake(0, actionSheetY, kDeviceWidth, actionSheetHeight);
                     }
                     completion:^(BOOL finished) {}];
}

@end
